import type { App } from "vue";
import { useUserInfoStore } from "@/stores/userInfo";

const registerPermissionDirective = (app: App) => {
  app.directive("permission", (el, binding) => {
    /*
      el 绑定指令的DOM元素
      binding 是一个对象，
        binding.value 就是指令表达式的值

      v-permission="'btn.user.add'"
    */
    const userInfoStore = useUserInfoStore();
    const { isAll, permission } = binding.value;
    if (isAll) {
      if (permission.every((p: string) => userInfoStore.buttons.includes(p))) {
        return;
      }
    } else {
      if (permission.some((p: string) => userInfoStore.buttons.includes(p))) {
        return;
      }
    }
    el.remove();
  });
};

export default registerPermissionDirective;
